package com.ews.onlineexchange.controller.assess;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.enterprise.inject.Model;
import javax.inject.Inject;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.Join;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import com.ews.onlineexchange.model.assess.AssessRecord;
import com.ews.onlineexchange.model.assess.Dict;
import com.ews.onlineexchange.model.assess.PeriodType;
import com.ews.onlineexchange.service.EntityQuery;
import com.ews.onlineexchange.service.UserService;
import com.ews.onlineexchange.util.PeriodUtils;

@Model
public class AssessRecordForSupplierList extends EntityQuery<AssessRecord> {

	/**
	 * 
	 */
	private static final long	serialVersionUID	= 1L;
	
	@Inject
	UserService 				userService;
	
	private PeriodType			periodType;
	private Integer				periodYear;
	private Integer				periodSeqNo;

	@Override
	protected List<Predicate> getRestrictions( CriteriaBuilder cb, Root<AssessRecord> entity ) {

		List<Predicate> restrictions = super.getRestrictions( cb, entity );
		
		Join<Object, Object> dict = entity.join( "dict" );
		
		restrictions.add( cb.isTrue( dict.join( "assessClass" ).get("allowSupplierApply") ) );
		restrictions.add( cb.equal( dict.get( "periodType" ), getPeriodType() ) );
		restrictions.add( cb.isFalse( entity.get( "override" ) ) );
		restrictions.add( cb.equal( entity.join( "serviceSupplier" ).get( "id" ), userService.getLoginedUser().getId() ) );
		
		if ( getPeriodType().equals( PeriodType.Year ) ) {
			restrictions.add( cb.equal( entity.get( "periodYear" ), getPeriodYear() ) );
		} else if ( getPeriodType().equals( PeriodType.Season ) || getPeriodType().equals( PeriodType.Month ) ) {
			restrictions.add( cb.equal( entity.get( "periodYear" ), getPeriodYear() ) );
			restrictions.add( cb.equal( entity.get( "periodSeqNo" ), getPeriodSeqNo() ) );
		}
		
		return restrictions;

	}
	
	public Map<Dict, AssessRecord> getResultMap() {
		List<AssessRecord> resultList = getResultList();
		Map<Dict, AssessRecord> resultMap = new HashMap<Dict, AssessRecord>();
		for ( AssessRecord assessRecord : resultList ) {
			resultMap.put( assessRecord.getDict(), assessRecord );
		}
		return resultMap;
	}

	public AssessRecordForSupplierList() {
		setMaxResults( null );
	}

	public PeriodType getPeriodType() {
		return periodType;
	}

	public void setPeriodType( PeriodType periodType ) {
		this.periodType = periodType;
	}

	public Integer getPeriodYear() {
		if ( this.periodYear == null ) {
			return PeriodUtils.getPeriodYear( getPeriodType() );
		}
		
		return periodYear;
	}

	public void setPeriodYear( Integer periodYear ) {
		this.periodYear = periodYear;
	}

	public Integer getPeriodSeqNo() {
		if ( this.periodSeqNo == null ) {
			return PeriodUtils.getPeriodSn( getPeriodType() );
		}
		return periodSeqNo;
	}

	public void setPeriodSeqNo( Integer periodSeqNo ) {
		this.periodSeqNo = periodSeqNo;
	}

}
